/*
 * New BSD License
 * 
 * Copyright (c) 2008, Dan Crawford, Philip Nichols, Jacob Siegel, Tim Stefanski, Justin Williamson All rights reserved.
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the distribution.
 * Neither the name of the Artemis Group nor the names of its contributors may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE. 
 * 
 * Project Artemis
 */


package ALDITS;

import java.awt.Graphics;
import java.awt.Color;
/*
 * Test code to present a circle circumscribed by a square, ie the area between 
 * is filled.  !!Draws from top left corner!!
 * 
 * Contains private class points to facilitate computation
 * Runtime is probably ridiculous.
 * 
 * Number of points in the circle calculated can be adjusted
 * by the "int inc" found in method genCircle ().
 * 
 * This opbject is drawn/represented by outlining four points of the square
 * then the points around the circle IN ORDER, then calling fillPolygon().
 */

public class Scope {
	int x;
	int y;
	int radius;
	int [] xPoints;
	int [] yPoints;
	//Constructor, input is the radius of inner circle (ie. half width of square)
	//Circle points are calculated at construction.
	public Scope (int rad, int xer, int yer)
	{
		radius = rad;
		x = xer;
		y = yer;
		setScope();
	}
	//once the scope points have been calculated, this takes them and draws
	public void drawScope (Graphics g)
	{
		g.fillPolygon(xPoints, yPoints, xPoints.length);
	}
	//helper function for debugging, list all points in polygon
	public void seePoints()
	{
		for (int i = 0; i < xPoints.length; i++)
		{
			System.out.print(xPoints[i]);
			System.out.println("\t" + yPoints[i]);
		}
	}
	//generates all points in polygon
	public void setScope()
	{
		int nPoints = 6 + 4*radius;
		Points newPoints = genCircle();
		xPoints = new int [nPoints];
		yPoints = new int [nPoints];
		xPoints [3] = 0 + x;
		yPoints [3] = 0 + y;
		xPoints [0] = 0 + x;
		yPoints [0] = radius*2 + y;
		xPoints [1] = radius*2 + x;
		yPoints [1] = radius*2 + y;
		xPoints [2] = radius*2 + x;
		yPoints [2] = 0 + y;
		int [] xCirc = newPoints.returnX();
		int [] yCirc = newPoints.returnY();
		for (int i = 4; i < nPoints; i++)
		{
			xPoints [i] = xCirc[i - 4];
			yPoints [i] = yCirc[i - 4];
		}
	}
	//subFunction of setScope, generates points of the circle
	// in order, from left tangent to square in clockwise order.
	private Points genCircle ()
	{
		int inc = 1;
		int half = (2*radius + 1)/inc;
		int points [] = new int[2*half];
		int xpoints [] = new int[2*half];
		int count = 0;
		for (int i = -radius; i <= radius; i += inc)
		{
			xpoints [count] = i+radius + x;
			xpoints [2*half - count -1] = xpoints[count];
			int offset = (int) (Math.sqrt(Math.pow(radius, 2) - Math.pow(i,2)));
			points[count] = radius - offset + y;
			points[2*half - count -1] = radius + offset + y;
			count++;
		}
		Points cirPoints = new Points (xpoints, points);
		return cirPoints;
	}
	//subClass used to represent and pass the two list of coordiates.
	//only a helper class, synonomous to the Java class Point but works with arrays
	private class Points
	{
		int [] x;
		int [] y;
		Points (int [] x, int [] y)
		{
			this.x = x;
			this.y = y;
		}
		int [] returnX ()
		{
			return x;
		}
		int [] returnY()
		{
			return y;
		}
	}
}
